package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工控制器")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;




    /**
     * 员工新增
     * @param employeeDTO
     * @return // 17:50
     */
    @ApiOperation("新增员工")
    @PostMapping
    public Result<String> addEmp(@RequestBody EmployeeDTO employeeDTO) {
        log.info("[控制器] thread:{}",Thread.currentThread().getId());
        // 1.打印
        log.info("[新增员工] emp:{}", employeeDTO);
        // 2.调用
        Integer tag = employeeService.addEmp(employeeDTO);

        // 3. 响应
        return tag>0?Result.success("新增成功"):Result.error("新增失败");
    }

    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return  11:40 - 11:57 = 17 min
     */
    @ApiOperation("员工分页查询")
    @GetMapping("/page")
    public Result<PageResult> findByPage(EmployeePageQueryDTO employeePageQueryDTO) {
        // 打印
        log.info("[分页查询] empDTO:{}",employeePageQueryDTO);
        // 调用
        PageResult pageResult = employeeService.findByPage(employeePageQueryDTO);

        // 响应
        return Result.success(pageResult);
    }


    /**
     * 员工启用/禁用 状态更新
     * @param status
     * @param id
     * @return 16:04 - 16:12 = 8min
     */
    @PostMapping("/status/{status}")
    public Result<String> enableOrNotStatus(@PathVariable Integer status, Long id) {
        log.info("[启动/禁用 员工状态] status:{} id:{}",status,id);

        Integer tag = employeeService.enableOrNotStatus(status,id);

        return Result.success(tag>0?"更新成功":"更新失败");
    }


    /**
     * 根据id查询用户西悉尼
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<EmployeeVO> findById(@PathVariable Long id) {
        // 打印
        log.info("[根据ID查询用户信息] id:{}",id);
        // 调用
        EmployeeVO employeeVO = employeeService.findById(id);
        // 执行
        return Result.success(employeeVO);

    }

    /**
     * 更新用户信息
     * @param employeeDTO
     * @return 17:00 - 17:22 = 22 min
     */
    @PutMapping
    public Result<String> updateById(@RequestBody EmployeeDTO employeeDTO) {
        log.info("[更新用户信息] emp:{}",employeeDTO);
        Integer tag = employeeService.updateById(employeeDTO);
        return Result.success(tag>0?"更新成功":"更新失败");
    }


    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);



        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("登出")
    public Result<String> logout() {
        return Result.success();
    }

}
